package com.wz.project.system.prjgroup.service.impl;

import com.wz.common.utils.security.ShiroUtils;
import com.wz.common.utils.text.Convert;
import com.wz.framework.web.domain.BaseEntity;
import com.wz.project.system.dept.domain.Dept;
import com.wz.project.system.dept.mapper.DeptMapper;
import com.wz.project.system.dept.service.IDeptService;
import com.wz.project.system.employee.domain.Employee;
import com.wz.project.system.employee.domain.EmployeeVO;
import com.wz.project.system.employee.mapper.EmployeeMapper;
import com.wz.project.system.employee.mapper.EmployeeVoMapper;
import com.wz.project.system.prjgroup.Repository.PrjgroupRepository;
import com.wz.project.system.prjgroup.domain.Prjgroup;
import com.wz.project.system.prjgroup.domain.PrjgroupEmployee;
import com.wz.project.system.prjgroup.entity.PrjgroupEntity;
import com.wz.project.system.prjgroup.mapper.PrjGroupMapper;
import com.wz.project.system.prjgroup.mapper.PrjgroupEmployeeMapper;
import com.wz.project.system.prjgroup.service.PrjGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class PrjGroupServiceImpl implements PrjGroupService {

    @Autowired
    private PrjGroupMapper prjGroupMapper;

    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private PrjgroupEmployeeMapper prjgroupEmployeeMapper;

    @Autowired
    private PrjgroupRepository prjgroupRepository;

    @Autowired
    private IDeptService deptService;

    @Autowired
    private EmployeeVoMapper employeeVoMapper;
    @Override
    public List<Prjgroup> selectPrjGroupList(Prjgroup prjgroup) {
        List<Prjgroup> prjgroups = prjGroupMapper.selectPrjGroupList(prjgroup);
        //查询部门列表
        List<Integer> deptIds = prjgroups.stream().map(Prjgroup::getDeptId).collect(Collectors.toList());
        if (deptIds.size()>0){
            List<Dept> depts = deptMapper.selectDeptListByDeptId(deptIds);
            Map<Long, String> collect = depts.stream().collect(Collectors.toMap(Dept::getDeptId, Dept::getDeptName));
            for (Prjgroup item : prjgroups) {
                item.setDeptName(collect.get(Long.valueOf(item.getDeptId())));
            }
        }

        return prjgroups;
    }

    @Override
    public List<PrjgroupEntity> selectPrjGroupListJpa(Prjgroup prjgroup, Integer pageNum, Integer pageSize) {

        List<PrjgroupEntity> prjgroups = prjgroupRepository.findALlPrjgroup(prjgroup);

        List<Integer> deptIds = prjgroups.stream().map(PrjgroupEntity::getDeptId).collect(Collectors.toList());
        if (deptIds.size()>0){
//            List<Dept> depts = prjgroupRepository.findByDeptIn(deptIds);
//            Map<Long, String> collect = depts.stream().collect(Collectors.toMap(Dept::getDeptId, Dept::getDeptName));
//            for (PrjgroupEntity item : prjgroups) {
//                item.setDeptName(collect.get(Long.valueOf(item.getDeptId())));
//            }
        }
        return prjgroups;
    }

    @Override
    public int insertOne(Prjgroup prjgroup) {
        prjgroup.setCreateBy(ShiroUtils.getLoginName());
        prjgroup.setCreateTime(new Date());
        prjgroup.setDelFlag(BaseEntity.DEL_FLAG);
        int rows = prjGroupMapper.insertOne(prjgroup);
        return rows;
    }

    @Override
    public Prjgroup selectOneById(Integer prigroupId) {

        Prjgroup prjgroup = prjGroupMapper.selectOneById(prigroupId);
        List<Dept> depts = deptMapper.selectDeptListByDeptId(new ArrayList<Integer>() {{
            add(prjgroup.getDeptId());
        }});

        prjgroup.setDeptName(depts.get(0).getDeptName());
        return prjgroup;
    }

    @Override
    public int update(Prjgroup prjgroup) {
        prjgroup.setUpdateBy(ShiroUtils.getLoginName());
        prjgroup.setUpdateTime(new Date());
        return prjGroupMapper.update(prjgroup);
    }

    @Override
    public int deletByIds(String ids) {
        Long[] prjgroupIds = Convert.toLongArray(ids);
        return prjGroupMapper.deleteByIds(prjgroupIds);
    }

    @Override
    public List<EmployeeVO> selectEmployeesByPrjgroupId(Prjgroup prjgroup,Employee employee) {
        //查询该项目组所在部门下的员工列表
//        List<Employee> employees = employeeMapper.selectEmployeeList(new Employee() {{
//            if (prjgroup.getDeptId() == 12 || prjgroup.getDeptId() == 1 || prjgroup.getDeptId() == 2){
//                setDeptId(null);
//            }else {
//                setDeptId(prjgroup.getDeptId());
//            }
//        }});
//        List<Dept> depts = deptService.selectDeptList(new Dept());
//        Map<Long, Dept> collect = depts.stream().collect(Collectors.toMap(Dept::getDeptId, Function.identity()));
        //查询已经分配在该项目组下的员工
        List<PrjgroupEmployee> currentEmps = prjgroupEmployeeMapper.selectListByPrjgroupId(prjgroup.getPrjgroupId());
        List<Integer> ids = currentEmps.stream().map(PrjgroupEmployee::getEmployeeId).collect(Collectors.toList());
//        List<EmployeeVO> employeeVOS = new ArrayList<>();
        List<EmployeeVO> employeeVOS = employeeVoMapper.selectEmployeeList(employee);
        //设置flag标识
//        for (Employee employee : employees) {
//            EmployeeVO employeeVO = new EmployeeVO();
//            BeanUtils.copyProperties(employee,employeeVO);
//            employeeVOS.add(employeeVO);
//            employeeVO.setDeptName(collect.get(Long.valueOf(employeeVO.getDeptId())).getDeptName());
//            if (ids.contains(employeeVO.getEmployeeId())){
//                employeeVO.setFlag(true);
//            }
//        }
        for (EmployeeVO employeeVO : employeeVOS) {
            if (ids.contains(employeeVO.getEmployeeId())){
                employeeVO.setFlag(true);
            }
        }
        return employeeVOS;

    }

    /**
     * 保存员工和项目组的关联关系
     * @param prjgroupId 项目组id
     * @param employeeIds 员工ids
     * @return
     */
    @Override
    public int saveEmployeesAndPrjgroup(Integer prjgroupId, String employeeIds) {
        //先将原先数据删除
        prjgroupEmployeeMapper.deleteByPrjgroupId(prjgroupId);
        //插入新的数据
        List<PrjgroupEmployee> list = new ArrayList<>();
        String[] strings = employeeIds.split(",");
        if (strings.length>0){
            for (String string : strings) {
                PrjgroupEmployee prjgroupEmployee = new PrjgroupEmployee() {{
                    if (string != null){
                        setPrjgroupId(prjgroupId);
                        setEmployeeId(Integer.valueOf(string));
                    }
                }};
                list.add(prjgroupEmployee);
            }
            return prjgroupEmployeeMapper.insertList(list);
        }else {
            return 0;
        }
    }

    @Override
    public List<Prjgroup> selectPrjgroupByDeptId(Integer deptId) {


        return prjGroupMapper.selectPrjgroupByDeptId(deptId);
    }
}
